﻿@model Proggy.Web.Cms.ViewModels.SortChildContentViewModel
@{
    // add js
    Html.AddToBackEndContent("~/Areas/Proggy/Content/Scripts/Proggy.ContentEdit.js", BackEndContentType.JavaScript);
    Html.AddToBackEndContent("~/Areas/Proggy/Content/Scripts/Proggy.ContentSort.js", BackEndContentType.JavaScript);
    
    // name
    string title = "Sort";
    string name = "";

    // check
    if (Model.ParentExists)
    {
        name = (Model.ParentNode.HasDraft ? Model.ParentNode.DraftNode.Name : Model.ParentNode.Name);
        title = "Sort child content of " + name;
    }

    //
    ViewBag.Title = title;
}
<h2>@ViewBag.Title</h2>
<div class="proggy-sort">
    @if (!Model.ParentExists)
    { 
        <h3>
            Content ID @(Model.RequestedParentId)
            does not match any content.</h3>
        <p>@Html.ActionLink("Return >>", "Index")</p>
    }
    else if (!Model.HasChildren)
    { 
        <h3>
            Content "@name" has no sortable content.</h3>
        <p>@Html.ActionLink("Return >>", "Index", new { open = Model.RequestedParentId, selected = Model.RequestedParentId })</p>
    }
    else
    {
        /*
         * ALL IS WELL...
         */

        <h3>
            Drag and drop to sort children:</h3>
        using (Html.BeginForm())
        {
            @:@Html.ValidationSummary(false)
        <div class="proggy-save-bar">
            <input type="submit" class="proggy-button-go" value="Save" />
            |
            @Html.ActionLink("Return to content", "Index", new { success=5, open = Model.ParentNode.PublishableNodeId, selected = Model.ParentNode.PublishableNodeId }, new { @class = "proggy-button-stop" })
        </div> 
        
        <div id="proggy_sortable_children">
            @{
            // start counter
            var i = 0;

            // iterate children
            foreach (var child in Model.ChildNodesOrdered)
            {
                <div class="proggy-sort-child">
                    @Html.HiddenFor(m => m.PostSortedChildren[i])
                    @(child.HasDraft ? child.DraftNode.Name : child.Name)
                </div>
        
                // increase counter
                i++;
            }
            }
        </div>
        <div class="proggy-save-bar proggy-save-bar-bottom">
            <input type="submit" class="proggy-button-go" value="Save" />
            |
            @Html.ActionLink("Return to content", "Index", new { success = 5, open = Model.ParentNode.PublishableNodeId, selected = Model.ParentNode.PublishableNodeId }, new { @class = "proggy-button-stop" })
        </div> 
        
        }

    }
</div>
<script type="text/javascript">
    
    // wait for types
    jQuery(function () {

        // check for errors
        var edit = new Proggy.ContentEdit();
        edit.CheckForValidationErrors();
      
        // enable sort
        var sort = new Proggy.ContentSort();
        sort.Init();

    });    
</script>
